package function

import (
	"GraduationProject/public/models"
	"GraduationProject/public/params"
	"GraduationProject/worker/neteasy_music/fetcher/neteasy_music"
	"GraduationProject/worker/neteasy_music/neteasy_music_conf"
	"GraduationProject/worker/neteasy_music/parser"
	"GraduationProject/worker/worker_tools"
	"fmt"
)

func Comments(seed models.Record) (result params.Result) {
	// https://music.163.com/weapi/v1/resource/comments/R_SO_4_419596181?csrf_token=
	content, nextPs, err := neteasy_music.NetEasyMusicClient(seed.Url, seed.Params)
	if err != nil {
		fmt.Println("NetEasyMusicClient error:", err)
		if nextPs != "" {
			return params.Result{
				Result: []models.Record{
					{
						SeedID:       seed.SeedID,
						Url:          seed.Url,
						Params:       nextPs,
						FunctionName: seed.FunctionName,
						Error:        err.Error(),
						Result:       "",
					},
				},
			}
		}
		return
	}

	records, more := parser.Comments(seed, content)
	result.Result = records
	if more {
		result.Seeds = []models.Record{{
			SeedID:       seed.SeedID,
			Url:          seed.Url,
			Params:       nextPs,
			FunctionName: neteasy_music_conf.NetEasyMusicComments,
		}}
	}
	if !visited {
		worker_tools.Visited(seed)
	}
	return
}
